package com.czl.module_work.viewModel

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.*
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.czl.base.util.ToastHelper


/**
 * 创建日期：2021/12/27  14:04
 * 类说明:
 * @author：86152
 */
class TakeCareRecordViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {


    var page = 1

    val pageSize = 10

    var filterParams = TakeCareRecordFilterParams("", "", "", "")

    val deviceNoText = ObservableField<String>()
    val search = BindingCommand<String> { t ->
        filterParams.deviceNo = t ?: ""
        refresh()
    }

    val btnScanClick: BindingCommand<Void> = BindingCommand(BindingAction {
        PermissionUtils.permission(PermissionConstants.CAMERA)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    startContainerActivity(AppConstants.Router.Work.F_WORK_SCAN)
                }

                override fun onDenied() {
                    ToastUtils.showShort("没有相机权限!")
                }
            }).request()
    })
    val btnBuildingClick: BindingCommand<Void> = BindingCommand(BindingAction {
        getBuildingList()
    })

    val btnFloorClick: BindingCommand<Void> = BindingCommand(BindingAction {
        getFloorList(filterParams.buildingId)
    })

    val btnDeviceTypeClick: BindingCommand<Void> = BindingCommand(BindingAction {
        getDeviceTypeList()
    })

    val uc = UiChangeEvent()

    class UiChangeEvent {

        val loadOrderEvent: SingleLiveEvent<List<TakeCareRecordBean.Data>> = SingleLiveEvent()

        val buildingFilterEvent: SingleLiveEvent<List<BuildingtBean>> = SingleLiveEvent()

        val floorFilterEvent: SingleLiveEvent<List<FloorBean>> = SingleLiveEvent()

        val deviceTypeFilterEvent: SingleLiveEvent<List<DeviceTypeBean>> = SingleLiveEvent()

        val clearOrderEvent: SingleLiveEvent<Void> = SingleLiveEvent()

    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        loadNext()
    })

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        refresh()
    })

    override fun setToolbarRightClick() {

    }

    fun setFloorFilter(floorId: String) {
        filterParams.floorId = floorId
        refresh()
    }

    fun setBuildingFilter(buildingId: String) {
        filterParams.buildingId = buildingId
        refresh()
    }

    fun setDeviceTypeFilter(deviceTypeId: String) {
        filterParams.pointTypeId = deviceTypeId
        refresh()
    }

    fun refresh() {
        page = 1
        uc.clearOrderEvent.call()
        getRecords()
    }

    fun loadNext() {
        page++
        getRecords()
    }

    fun getRecords() {
        model.takeCareOrderRecord(pageNum = page, pageSize = pageSize, filterParams)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<TakeCareRecordBean>>() {
                override fun onResult(t: BaseBean<TakeCareRecordBean>) {
                    if (t.data == null) return
                    if (t.code == 200) {
                        uc.loadOrderEvent.postValue(t.data!!.list)
                    } else {
                        uc.loadOrderEvent.postValue(null)
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    uc.loadOrderEvent.postValue(null)
                    showErrorToast(msg)
                }
            })
    }

    fun getBuildingList() {
        model.listBuilding()
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<BuildingtBean>>>() {
                override fun onResult(t: BaseBean<List<BuildingtBean>>) {
                    if (t.data == null) return
                    if (t.code == 200) {
                        uc.buildingFilterEvent.postValue(t.data)
                    } else {
                        uc.buildingFilterEvent.postValue(null)
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    uc.buildingFilterEvent.postValue(null)
                    showErrorToast(msg)
                }
            })
    }

    fun getFloorList(buildingId: String) {
        if (TextUtils.isEmpty(filterParams.buildingId)) {
            ToastHelper.showNormalToast("请选择楼栋!")
            return
        }
        model.listFloor(buildingId)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<FloorBean>>>() {
                override fun onResult(t: BaseBean<List<FloorBean>>) {
                    if (t.data == null) return
                    if (t.code == 200) {
                        uc.floorFilterEvent.postValue(t.data)
                    } else {
                        uc.floorFilterEvent.postValue(null)
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    uc.floorFilterEvent.postValue(null)
                    showErrorToast(msg)
                }
            })
    }

    fun getDeviceTypeList() {
        model.listPointType("")
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<DeviceTypeBean>>>() {
                override fun onResult(t: BaseBean<List<DeviceTypeBean>>) {
                    if (t.data == null) return
                    if (t.code == 200) {
                        uc.deviceTypeFilterEvent.postValue(t.data)
                    } else {
                        uc.deviceTypeFilterEvent.postValue(null)
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    uc.deviceTypeFilterEvent.postValue(null)
                    showErrorToast(msg)
                }
            })
    }
}